
///////////////////////////////////////////////////////////////////////////////
// BugForumNotifier

// Reference to the WRTKit user interface manager and main view.
var uiManager;
var mainView;
var loginView;
var aboutView;
var optionsView;

// Reference to controls in the main view.
var helloButton;
var nameField;

// Event handlers
var loginButtonPressed;

//about view label control
var aboutLabel;
//login view label control
var usernameTextField;
var passwordTextField;
var submitButton;

// Constants for menu item identifiers.
var MENU_ITEM_ABOUT = 2;
var MENU_ITEM_SHOW_BLOCKED = 1;
var MENU_ITEM_REFRESH = 0;
var MENU_ITEM_LOGIN = 3;

// Called from the onload event handler to initialize the widget.
function init() {
	
	//create about menu
	var aboutMenuItem = new MenuItem("About", MENU_ITEM_ABOUT);
	aboutMenuItem.onSelect = menuItemSelected;
	menu.append(aboutMenuItem);
	
	var loginMenuItem = new MenuItem("Login", MENU_ITEM_LOGIN);
	loginMenuItem.onSelect = menuItemSelected;
	menu.append(loginMenuItem);
	
    // set tab-navigation mode and show softkeys
    // (only if we are in the WRT environment)
    if (window.widget) {
        widget.setNavigationEnabled(false);
        menu.showSoftkeys();
    }
    
    // create UI manager
    uiManager = new UIManager();
    
    // create main view
    mainView = new ListView(null, "Hello World");
    
	//Create about view
	aboutView = new ListView(null, "Hello World");
	
	loginView = new ListView(null, "Login")
	usernameTextField = new TextField("email", "E-mail:");
    passwordTextField = new TextField("lozinka", "Lozinka:",null,"true");
	submitButton = new FormButton(null, "Prijava");
    loginView.addControl(usernameTextField);
	loginView.addControl(passwordTextField);
	submitButton.addEventListener("ActionPerformed", loginButtonPressed);
	loginView.addControl(submitButton);
	
	
	// add a text field to the view
    nameField = new TextField(null, "Enter your name");
    mainView.addControl(nameField);
    
    // add a button to the view
    helloButton = new FormButton(null, "Say Hello!");
    helloButton.addEventListener("ActionPerformed", helloButtonClicked);
    mainView.addControl(helloButton);
    
	// About lable control
	aboutLabel = new Label();
	aboutView.addControl(aboutLabel);
	
    // display the main view
    uiManager.setView(mainView);
}


function showNotification(infoText){
	uiManager.showNotification(2000, "info", infoText)
}
// Show main view.
function showMainView() {
    
    // set right softkey to "exit"
    if (window.widget) {
        menu.setRightSoftkeyLabel("", null);
    }
    
    // show the main view
    uiManager.setView(mainView);
}
// Callback for when menu items are selected.
function menuItemSelected(id) {
    switch (id) {
       	case MENU_ITEM_ABOUT:
			showAboutView();
			break;
		case MENU_ITEM_LOGIN:
			showLoginView();
			break;
    }
}

//Displays the About view
function showAboutView(){
	aboutLabel.setText("Naziv: BugForumNotifier\nVerzija: 0.1.0");
	
	setAboutViewSoftkeys();
	uiManager.setView(aboutView);
}

function showLoginView(){
	
	//setAboutViewSoftkeys();
	uiManager.setView(loginView);
}

// Sets the soft keys for about view.
function setAboutViewSoftkeys() {
    if (window.widget) {
        // set right soft key to "Ok" (returns to main view)
        menu.setRightSoftkeyLabel("Ok", showMainView);
    }
}

// Called when the hello-button is clicked.
function helloButtonClicked(event) {
    var name = nameField.getText();
    if (name.length == "") {
        uiManager.showNotification(3000, "warning", "Please enter your name!");
    } else {
        uiManager.showNotification(3000, "info", "Hello " + name + "!");
    }
}
